package com.scoy.honeymei.activity.home;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.a_checkseaat.SeatActivity;
import com.scoy.honeymei.activity.mall.BookActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.FilmDetailBean;
import com.scoy.honeymei.custem.MyJzvdStd;
import com.scoy.honeymei.dialog.ShareDialog;
import com.scoy.honeymei.dialog.ShowFullDialog;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.TimeUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;

/**
 * @ClassName: GoodsDetailActivity$
 * @Description: 电影详情
 * @Author: GaoZhan
 * @CreateDate: 2019/9/3/0003$ 上午 10:08$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/3/0003$ 上午 10:08$
 * @UpdateRemark: 更新内容
 */
public class FilmDetailActivity extends BaseActivity {


    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.film_name_tv)
    TextView filmNameTv;
    @BindView(R.id.film_address_tv)
    TextView filmAddressTv;
    @BindView(R.id.film_time_tv)
    TextView filmTimeTv;
    @BindView(R.id.film_moneysign_tv)
    TextView filmMoneysignTv;
    @BindView(R.id.film_money_tv)
    TextView filmMoneyTv;
    @BindView(R.id.fd_web)
    WebView fdWeb;
    @BindView(R.id.fd_book_tv)
    TextView fdBookTv;
    @BindView(R.id.film_img_iv)
    ImageView filmImgIv;
    @BindView(R.id.film_endtime_tv)
    TextView filmEndtimeTv;
    /**
     * 提示
     */
    TextView notSureTv;
    private FilmDetailActivity mContext;
    private int fId;
    private MyJzvdStd jzvdStd;
    private String fxUrl;
    private String fxImage;
    private String fxName;
    private String fxContent;
    private int cinemaId;
    private boolean isJump = false;
    private int seconds = 3;
    CountDownTimer timer = new CountDownTimer(3000, 1000) {
        public void onTick(long millisUntilFinished) {
            seconds = (int) (millisUntilFinished / 1000);
            notSureTv.setText(String.valueOf(seconds + 1));

        }

        public void onFinish() {
            isJump = true;
            notSureTv.setText("确定");
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiy_detail_film);
        ButterKnife.bind(this);
        mContext = FilmDetailActivity.this;
        fId = getIntent().getIntExtra("fId", 0);
        cinemaId = getIntent().getIntExtra("cinemaId", 0);
        jzvdStd = findViewById(R.id.jz_video);

        initNormal();

        httpDate();

    }

    @Override
    public void initNormal() {
        titleTv.setText(R.string.filmdetail);

        MyUtil.setStatusBar(mContext, getWindow(), true, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        signTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_share_gray, 0, 0, 0);
        signTv.setText("分享");

        Uri uri = getIntent().getData();
        if (uri != null) {
            String tttid = uri.getQueryParameter("id");
            try {
                fId = Integer.parseInt(tttid);
            } catch (Exception ignored) {
            }
        }

    }

    private void httpDate() {
        HttpParams params = new HttpParams();
        params.put("id", fId);
        HpGo.newInstance().HttpGo(this, MyUrl.FILM_DETAIL, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                FilmDetailBean filmDetailBean = new Gson().fromJson(o, FilmDetailBean.class);
                FilmDetailBean.MovieBean movie = filmDetailBean.getMovie();

                String imagetop = movie.getImage();
                if (imagetop != null && !"".equals(imagetop)) {
                    filmImgIv.setVisibility(View.VISIBLE);
                    jzvdStd.setVisibility(View.GONE);
                    GlideImgUtil.glidePicNo(mContext, imagetop, filmImgIv);
                    filmImgIv.setOnClickListener(v -> {
                        ShowFullDialog.newInstance(new ArrayList<String>() {{
                            add(imagetop);
                        }}).show(getSupportFragmentManager(), 0 + "");
                    });

                } else {
                    filmImgIv.setVisibility(View.GONE);
                    jzvdStd.setVisibility(View.VISIBLE);
                    jzvdStd.setUp(movie.getVideo(), movie.getTitle());
                    //      jzvdStd.thumbImageView.setImageURI(Uri.parse(movie.getVideo()));
                 /*   Bitmap bitmap = VideoUtil.voidToFirstBitmap(movie.getImage());
                    String sfile = VideoUtil.bitmapToStringPath(mContext, bitmap);*/
                    jzvdStd.thumbImageView.setImageResource(R.mipmap.ic_hm_logo);
                }
                filmNameTv.setText(movie.getTitle());

                filmAddressTv.setText("厅号：" + movie.getFyt_no());
                filmTimeTv.setText("开场时间：" + TimeUtil.getLongToDatePHP(movie.getStime(), "yyyy-MM-dd HH:mm"));
                filmEndtimeTv.setText("散场时间：" + TimeUtil.getLongToDatePHP(movie.getEtime(), "yyyy-MM-dd HH:mm"));
                filmMoneyTv.setText(movie.getPrice());

                if (CheckUtil.checkIfUrl(movie.getDetail_url())) {
                    initWeb(fdWeb, movie.getDetail_url());

                }

                fxUrl = movie.getFx_url();
                fxImage = movie.getFx_image();
                fxName = movie.getFx_title();
                fxContent = movie.getFx_miaoshu();
            }

        });

    }

    @OnClick({R.id.back_iv, R.id.fd_book_tv, R.id.sign_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.sign_tv:
                ShareDialog.newInstance(fxImage, fxName, fxContent, fxUrl).show(getSupportFragmentManager(), "");

                break;
            case R.id.fd_book_tv:
                onNotice();
                break;
        }
    }

    public void onNotice() {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_notice, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        notSureTv = view.findViewById(R.id.not_sure_tv);
        WebView notWeb = view.findViewById(R.id.not_web);

        notTitleTv.setText("淘票温馨提示");
        initWeb(notWeb, MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=6");

        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //  lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.width = (int) getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        notSureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isJump) {
                    dialogNotice.dismiss();
                    Intent intent = new Intent(mContext, SeatActivity.class);
                    intent.putExtra("fId", fId);
                    intent.putExtra("cinemaId", cinemaId);
                    startActivityForResult(intent, 0x871);
                }

            }
        });

        dialogNotice.show();
        timer.start();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871:
                startActivity(new Intent(mContext, BookActivity.class));
                finish();
                break;
        }
    }
    //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
